function poseReformat( tracksfile, outdir )
%poseReformat - Reformats deva's pose into a simpler 15 pts format that is
% more compatible with the Kinect's representation.
%
%           1                              1
%           |                              |
%      3----2----15                   3----2----6
%     / \       /  \                 / \\      /  \
%    /   |     |    \               //  \\   //   \\
%   4    8     20    16            //    \\ //     \\
%   |    |     |     |             /       9        \
%   5    9     21    17            4     // \\      7
%   |    |     |     |      ==>    |    //   \\     |
%   6    10    22    18            |    10    13    |
%   |    |     |     |             |    |     |     |
%   7    11    23    19            5    |     |     8
%        |     |                        |     |
%        12    24                       11    14
%        |     |                        |     |
%        13    25                       |     |
%        |     |                        |     |
%        14    26                       12    15
%
%      DEVA FORMAT                     NEW FORMAT

% Examples:
%   tracksfile = ['/Users/trananh/Desktop/gestures/KTH/ground-truth/' ...
%     'walking/person01_walking_d1_uncomp/00001-00076/skeleton/tracks.txt'];
%   outdir = ['/Users/trananh/Desktop/gestures/KTH/ground-truth/' ...
%     'walking/person01_walking_d1_uncomp/00001-00076/skeleton-tracks/'];
  
  % Create dir for output if necessary
  if ~strcmp(outdir(end), '/')
    outdir = [outdir '/'];
  end
  if ~exist(outdir, 'dir')
    mkdir(outdir);
  end
  
  % Set default out prefix
  prefix = 'person';
  
  % Load matrix
  fprintf('Reformating pose tracks: %s\n', tracksfile);
  data = load(tracksfile);
  
  % No tracks => nothing to output
  if isempty(data)
    return
  end
  
  % Deva's format by default should be: 26 boxes, score, frame, trackid
  numBoxes = (size(data,2) - 3) / 4;
  assert(numBoxes == 26), 'Invalid pose tracks format!';
  
  % Iterate over each track
  trackIDs = unique(data(:,end));
  for id = sort(trackIDs)
    % Grab track and sort by frames
    track = data(data(:,end) == id,:);
    track = sortrows(track,size(track,2)-1);
  
    % Grab the columns
    x1 = track(:,1:4:end-3);
    y1 = track(:,2:4:end-3);
    x2 = track(:,3:4:end-3);
    y2 = track(:,4:4:end-3);
    frames = track(:,end-1);
    assert(issorted(frames));
    assert(unique(track(:,end)) == 1 && track(1,end) == id);

    % Calculate center points
    x = (x1 + x2) / 2;
    y = (y1 + y2) / 2;

    % Reformat the pose
    result = [frames, ...
              x(:,1), y(:,1), ...                           % Head
              (x(:,3)+x(:,15))/2, (y(:,3)+y(:,15))/2, ...   % Neck
              x(:,3),y(:,3), ...                            % Lshoulder
              x(:,5),y(:,5), ...                            % Lelbow
              x(:,7),y(:,7), ...                            % Lhand
              x(:,15),y(:,15), ...                          % Rshoulder
              x(:,17),y(:,17), ...                          % Relbow
              x(:,19),y(:,19), ...                          % Rhand
              (x(:,8)+x(:,9)+x(:,20)+x(:,21))/4, ...        % Torso
                (y(:,8)+y(:,9)+y(:,20)+y(:,21))/4, ...
              x(:,10),y(:,10), ...                          % Lhip
              x(:,12),y(:,12), ...                          % Lknee
              x(:,14),y(:,14), ...                          % Lfoot
              x(:,22),y(:,22), ...                          % Rhip
              x(:,24),y(:,24), ...                          % Rknee
              x(:,26),y(:,26) ...                           % Rfoot
              ];
    
    % Write to out file
    filename = [outdir prefix '_' num2str(id) '.txt'];
    fid = fopen(filename, 'w');
    heading = ['Frame,Head_x,Head_y,Neck_x,Neck_y,'...
                'Lshoulder_x,Lshoulder_y,Lelbow_x,Lelbow_y,'...
                'Lhand_x,Lhand_y,Rshoulder_x,Rshoulder_y,'...
                'Relbow_x,Relbow_y,Rhand_x,Rhand_y,Torso_x,Torso_y,'...
                'Lhip_x,Lhip_y,Lknee_x,Lknee_y,Lfoot_x,Lfoot_y,'...
                'Rhip_x,Rhip_y,Rknee_x,Rknee_y,Rfoot_x,Rfoot_y'];
    fprintf(fid, '%s\n', heading);
    fclose(fid);
    dlmwrite(filename, result, 'delimiter', ',', 'precision', '%.3f', ...
      '-append');
    
  end
  
end
